package com.kasinf.sample.protect.controller;

import com.kasinf.framework.common.pojo.CommonResult;
import com.kasinf.framework.common.util.json.JsonUtils;
import com.kasinf.framework.crypto.core.annotation.Crypto;
import com.kasinf.framework.crypto.core.define.SecretKey;
import com.kasinf.framework.crypto.core.processer.HttpCryptoProcessor;
import com.kasinf.sample.protect.controller.vo.DataInfo;
import com.kasinf.sample.protect.controller.vo.SessionExchangeVO;
import com.kasinf.sample.protect.controller.vo.SessionVO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.util.Map;

/**
 * @Author : Kaiser
 * @Date: 2025/6/16  09:03
 */
@Slf4j
@RestController
@RequestMapping("crypto")
@RequiredArgsConstructor
public class CryptoController {

    private final HttpCryptoProcessor cryptoProcessor;

    @Crypto
    @PostMapping("updateData")
    public CommonResult<String> updateData(@RequestParam Map<String, Object> data){
        log.info(">>>> 解密后数据: {}", JsonUtils.toJsonString(data));
        return CommonResult.success("OK");
    }

    @Crypto
    @GetMapping("getData")
    public CommonResult<String> getData(String text){
        log.info(">>>> 解密后数据: {}", text);
        return CommonResult.success("OK");
    }

    @Crypto
    @PostMapping("createData")
    public CommonResult<DataInfo> createData(@RequestBody DataInfo data){
        log.info(">>>> 解密后数据: {}", JsonUtils.toJsonString(data));
        return CommonResult.success(data);
    }


    @PostMapping("key")
    public SessionVO getKey(@RequestBody SessionExchangeVO exchangeVO, HttpServletRequest request){
        HttpSession session = request.getSession();
        String sessionId = session.getId();
        exchangeVO.setSessionId(session.getId());
        SecretKey secretKey = cryptoProcessor.createSecretKey(sessionId, Duration.ofDays(1));
        SessionVO sessionVO = new SessionVO();
        sessionVO.setSessionId(sessionId);
        sessionVO.setSymmetricKey(secretKey.getSymmetricKey());
        return sessionVO;
    }

    /**
     * 交换公钥
     */
    @PostMapping("exchange")
    public SessionVO exchange(@RequestBody SessionExchangeVO exchangeVO, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String sessionId = session.getId();
        exchangeVO.setSessionId(session.getId());
        SecretKey secretKey = cryptoProcessor.createSecretKey(sessionId, Duration.ofDays(1));
        SessionVO sessionVO = new SessionVO();
        sessionVO.setSessionId(secretKey.getIdentity());
        // 这个是服务端公钥
        sessionVO.setPublicKey(secretKey.getPublicKey());
        // 这个是对称密钥
        sessionVO.setSymmetricKey(secretKey.getSymmetricKey());
        // 设置完之后交换客户端公钥
        cryptoProcessor.exchange(sessionId, exchangeVO.getPublicKey());
        return sessionVO;
    }
}
